package controller

import (
	"context"
	"mldong/api/base"
	{{.TableCamelNameLower}}Api "mldong/api/{{.TableCamelNameLower}}"
	"mldong/internal/modules/{{.ModuleName}}/logic"
	"mldong/internal/modules/{{.ModuleName}}/service"
	"mldong/utility"

	"github.com/gogf/gf/v2/frame/g"
)

func init() {
	// 注册控制器时自动注入依赖
	utility.RegisterController(&{{.TableCamelName}}Controller{
		// {{.TableCamelNameLower}}Service: utility.MustResolve(service.{{.TableCamelName}}ServiceType).(service.I{{.TableCamelName}}Service),
		// 控制层使用强依赖logic的方式，保证逻辑层的init方法比controller的方法先执行
		{{.TableCamelNameLower}}Service: utility.MustResolve(service.{{.TableCamelName}}ServiceType).(*logic.{{.TableCamelName}}Logic),
	})
}

type {{.TableCamelName}}Controller struct {
	{{.TableCamelNameLower}}Service service.I{{.TableCamelName}}Service
}

// 保存{{.Remark}}
func (ctrl *{{.TableCamelName}}Controller) Save(ctx context.Context, req *{{.TableCamelNameLower}}Api.SaveReq) (res *base.CommonResult, err error) {
	success, err := ctrl.{{.TableCamelNameLower}}Service.Save(req)
	if err != nil {
		return
	}
	if success {
		jsonData := base.Ok()
		g.RequestFromCtx(ctx).Response.WriteJson(jsonData)
	} else {
		jsonData := base.FailWithDefaultCode("保存{{.Remark}}失败")
		g.RequestFromCtx(ctx).Response.WriteJson(jsonData)
	}
	return
}

// 修改{{.Remark}}
func (ctrl *{{.TableCamelName}}Controller) Update(ctx context.Context, req *{{.TableCamelNameLower}}Api.UpdateReq) (res *base.CommonResult, err error) {
	success, err := ctrl.{{.TableCamelNameLower}}Service.Update(req)
	if err != nil {
		return
	}
	if success {
		jsonData := base.Ok()
		g.RequestFromCtx(ctx).Response.WriteJson(jsonData)
	} else {
		jsonData := base.FailWithDefaultCode("修改{{.Remark}}失败")
		g.RequestFromCtx(ctx).Response.WriteJson(jsonData)
	}
	return
}

// 删除{{.Remark}}
func (ctrl *{{.TableCamelName}}Controller) Remove(ctx context.Context, req *{{.TableCamelNameLower}}Api.RemoveReq) (res *base.CommonResult, err error) {
	success, err := ctrl.{{.TableCamelNameLower}}Service.Remove(req.Ids)
	if err != nil {
		return
	}
	if success {
		jsonData := base.Ok()
		g.RequestFromCtx(ctx).Response.WriteJson(jsonData)
	} else {
		jsonData := base.FailWithDefaultCode("删除{{.Remark}}失败")
		g.RequestFromCtx(ctx).Response.WriteJson(jsonData)
	}
	return
}

// 获取{{.Remark}}详情
func (ctrl *{{.TableCamelName}}Controller) Detail(ctx context.Context, req *{{.TableCamelNameLower}}Api.DetailReq) (res *{{.TableCamelNameLower}}Api.DetailRes, err error) {
	// 设置 JSON 数据
	jsonData, err := ctrl.{{.TableCamelNameLower}}Service.Detail(req.ID)
	if err != nil {
		return
	}
	g.RequestFromCtx(ctx).Response.WriteJson(base.Data(jsonData))
	return
}

// 分页查询{{.Remark}}列表
func (ctrl *{{.TableCamelName}}Controller) Page(ctx context.Context, req *{{.TableCamelNameLower}}Api.PageReq) (res *{{.TableCamelNameLower}}Api.PageRes, err error) {
	// 传递以m_开头的查询参数
	req.QueryParamMap = utility.GetQueryParamMap(ctx)
	jsonData, err := ctrl.{{.TableCamelNameLower}}Service.Page(req)
	if err != nil {
		return
	}
	g.RequestFromCtx(ctx).Response.WriteJson(base.Data(jsonData))
	return
}{{ if .IsTree }}
// 查询{{.Remark}}列表
func (ctrl *{{.TableCamelName}}Controller) List(ctx context.Context, req *{{.TableCamelNameLower}}Api.ListReq) (res *{{.TableCamelNameLower}}Api.ListRes, err error) {
	// 传递以m_开头的查询参数
	req.QueryParamMap = utility.GetQueryParamMap(ctx)
	jsonData, err := ctrl.{{.TableCamelNameLower}}Service.List(req)
	if err != nil {
		return
	}
	g.RequestFromCtx(ctx).Response.WriteJson(base.Data(jsonData))
	return
}
// 查询{{.Remark}}树
func (ctrl *{{.TableCamelName}}Controller) Tree(ctx context.Context, req *{{.TableCamelNameLower}}Api.TreeReq) (res *{{.TableCamelNameLower}}Api.TreeRes, err error) {
	// 传递以m_开头的查询参数
	req.QueryParamMap = utility.GetQueryParamMap(ctx)
	jsonData, err := ctrl.{{.TableCamelNameLower}}Service.Tree(req)
	if err != nil {
		return
	}
	g.RequestFromCtx(ctx).Response.WriteJson(base.Data(jsonData))
	return
}{{ end }}
